﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace AudioCore.Common
{
    public class HtmlUtils
    {
        private const string HttpTag = "http://";
        public static string GetImgUrl(string url)
        {
            string result = "<img src=\"" + url + "\" />";
            return result;
        }

        public static string GetLinkUrl(string href, string text)
        {
            string result = "<a href=\"" + href + "\">" + text + "</a>";
            return result;
        }

        public static string GetImgUrlFromClipboard()
        {
            string clipboard = Clipboard.GetText();
            if (clipboard.StartsWith(HttpTag))
                return GetImgUrl(clipboard);
            else
                return clipboard;
        }

        public static string InsertImgTagFromClipboard(string content, int index)
        {
            string clipboard = GetImgUrlFromClipboard();
            return InsertImgTag(content, index, clipboard);
        }

        public static string InsertImgTag(string content, int index, string imgTagUrl)
        {
            StringBuilder sb = new StringBuilder();

            string s = content.Substring(0, index);

            sb.Append(s);
            sb.Append(imgTagUrl);
            s = content.Substring(index);
            sb.Append(s);

            return sb.ToString();
        }

        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;
            //const string imgTag = "http://";

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }

                //if (i < source.Length - imgTag.Length && (source.Substring(i, imgTag.Length)).Equals(imgTag))
                //{
                //    inside = false;
                //    //continue;
                //}

                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

        
        public static string NormalizeText(string html)
        {
            string result = html.Replace(StringUtils.LF_CHAR, StringUtils.CRLF);
            result = result.Replace("&nbsp;", StringUtils.SPACE);
            result = result.Replace("&#160;", StringUtils.SPACE);
            result = result.Replace("''", "\"");
            result = result.Replace("'", "\"");
            return result;
        }

        public static string GetRequestContent(string url, string userAgent)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //request.UserAgent = "NokiaN73-2/3.0-630.0.2 Series60/3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1";
                request.UserAgent = userAgent;
                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string result = reader.ReadToEnd();
                response.Close();
                if (string.IsNullOrEmpty(result))
                {
                    return string.Empty;
                }
                else
                {
                    return result;
                }
            }
            catch
            {
                return string.Empty;
            }
        }

    }
}
